﻿(function() {
  var pageLoadTime;



  this.getDateTime = function() {
    var dateTime, day, hour, minute, month, now, second, year;
    now = new Date();
    year = now.getFullYear();
    month = now.getMonth() + 1;
    day = now.getDate();
    hour = now.getHours();
    minute = now.getMinutes();
    second = now.getSeconds();
    if (month.toString().length === 1) {
      month = '0' + month;
    }
    if (day.toString().length === 1) {
      day = '0' + day;
    }
    if (hour.toString().length === 1) {
      hour = '0' + hour;
    }
    if (minute.toString().length === 1) {
      minute = '0' + minute;
    }
    if (second.toString().length === 1) {
      second = '0' + second;
    }
    dateTime = year + '/' + month + '/' + day + ' ' + hour + ':' + minute + ':' + second;
    return dateTime;
  };

  this.UpdateCss = function(cssFile) {
    return $.get("css/stylesheet" + cssFile + ".css?" + pageLoadTime, function(data) {
      var styleSheetElement;
      styleSheetElement = $("#swappableStyleSheet");
      if (styleSheetElement[0].styleSheet) {
        styleSheetElement[0].styleSheet.cssText = data;
      } else {
        styleSheetElement.html(data);
      }
      return SubtractJS.Initialize().UpdateLayout();
    });
  };

  this.UpdateHtmlEditor = function(templateName) {
    var i, line, newHtmlSource, output, srcLen, _i;
    newHtmlSource = $("#" + templateName).html().split('\n');
    srcLen = newHtmlSource.length - 1;
    output = "";
    for (i = _i = 0; 0 <= srcLen ? _i <= srcLen : _i >= srcLen; i = 0 <= srcLen ? ++_i : --_i) {
      line = newHtmlSource[i];
      if (line.length > 8 && line.substring(0, 8) === "        ") {
        line = line.substring(8, line.length);
      }
      if ($.trim(line) !== "") {
        output += line;
        output += '\n';
      }
    }
    $("#htmlCode").val(output);
    return UpdatePreview();
  };

  this.UpdatePreview = function() {
    var newHtml;
    newHtml = $("#htmlCode").val();
    $("#previewArea").html(newHtml);
    return SubtractJS.Initialize().UpdateLayout();
  };

  $("form").submit(function(event) {
    return event.preventDefault();
  });

  $("#cssSelect").change(function() {
    return UpdateCss($(this).val());
  });

  $("#updateHtml").click(function() {
    return UpdatePreview;
  });

  $(".previewLink").click(function(event) {
    var id;
    event.preventDefault();
    id = $(this).attr("id");
    return UpdateHtmlEditor(id + "Sample");
  });

  $(UpdateCss("Square"), UpdateHtmlEditor("headerFooterSample"));

  pageLoadTime = getDateTime();

}).call(this);
